package com.pyb.common.sms.biz.service.impl;

import com.google.common.collect.Lists;
import com.pyb.common.sms.biz.model.SmsMultiSenderResult;
import com.pyb.common.sms.biz.model.SmsMultiSenderResultDetail;
import com.pyb.common.sms.biz.service.SmsMultiSender;
import com.pyb.common.sms.biz.service.SmsService;
import com.pyb.common.sms.biz.service.enums.SmsTemplete;
import com.pyb.common.utils.JsonUtil;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * SmsServiceImpl
 *
 * @author chuchen.zhang
 * @date 17-2-27 下午11:18
 */
@Service
public class SmsServiceImpl implements SmsService {

	private static final int APP_ID = 1400025430;

	private static final String APP_KEY = "5a31990d8d66ac4fb649f6f701a3d7f3";

	private static final Logger logger = LoggerFactory.getLogger(SmsServiceImpl.class);

	private SmsMultiSender multiSender = new SmsMultiSender(APP_ID, APP_KEY);

	@Override
	public boolean sendSecurityCode(String phoneNumber) {
		return sendMsg(SmsTemplete.SECURITY_TEMPLETE, Lists.newArrayList(phoneNumber), Lists.newArrayList("0012", "20")) > 0;
	}

	private int sendMsg(SmsTemplete smsTemplete, List<String> phoneNumberList, List<String> params) {
		if (!checkParamPass(smsTemplete, phoneNumberList, params)) {
			logger.error(
					"send msg param check error, smsTemple : {}, phoneList : {}, params : {}"
					, smsTemplete.getOriginalTemplete()
					, JsonUtil.toJsonString(phoneNumberList)
					, JsonUtil.toJsonString(params)
			);
			return 0;
		}
		int tmplId = smsTemplete.getTempleteId();
		SmsMultiSenderResult multiSenderResult = null;
		try {
			multiSenderResult = multiSender.sendWithParam("86", phoneNumberList, tmplId, params, "", "", "");
		} catch (Exception e) {
			logger.error("send msg error,  smsTemple : {}, phoneList : {}, params : {}"
					, smsTemplete.getOriginalTemplete()
					, JsonUtil.toJsonString(phoneNumberList)
					, JsonUtil.toJsonString(params)
					, e
			);
			return 0;
		}
		if (multiSenderResult != null) {
			return successCount(multiSenderResult);
		}
		return 0;
	}

	private boolean checkParamPass(SmsTemplete smsTemplete, List<String> phoneNumberList, List<String> params) {
		if (smsTemplete == null) {
			return false;
		}
		if (CollectionUtils.isEmpty(phoneNumberList)) {
			return false;
		}
		if (CollectionUtils.isEmpty(params)) {
			return false;
		}
		return true;
	}

	private int successCount(SmsMultiSenderResult result) {
		if (result == null) {
			return 0;
		}
		if (!isOk(result.result, result.errmsg)) {
			return 0;
		}
		int successCount = 0;
		for (SmsMultiSenderResultDetail detail : result.detail) {
			if ((detail != null) && isOk(detail.result, detail.errmsg)) {
				successCount ++;
			}
		}
		return successCount;
	}

	private boolean isOk(int result, String errorMsg) {
		return (result == 0) && (StringUtils.equals(errorMsg, "OK"));
	}
}
